﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using System.Data;
namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class HospitalOfMonth : BasePage
    {
        string strMosth = "";
        private bool hide = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                strMosth = String.IsNullOrEmpty(sq.Mid) ? "" : sq.Mid;
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            var model = new HZ.Data.QueryModel();
            model["AreaId"] = sq.AreaID;
            model["HospitalName"] = DAL.DAL_Helper.Replace(txtHospitalname.Text.Trim());
            model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;

            int t = 0;

            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            System.Data.DataTable dt = iNew.DayTime_SortSorec_HospitalName_GroupBy(model,this.AspNetPager1.CurrentPageIndex,this.AspNetPager1.PageSize, out t);

            AspNetPager1.RecordCount = t;
            //显示相应的字段
            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            gvwSortInfo.DataSource = dt;

            gvwSortInfo.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 0);

            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 9);

        }

        /// <summary>
        /// 合并表头
        /// 张雄 modify 2011.4.26
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份
                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "10"); //跨Column
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Text = ForMatDate(sq.Mid) + "</th></tr><tr>";


                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "医院";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "结算金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "退货金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());//2011-0329 ml 取消补贴
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[9].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[9].Text = "入库率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[10].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[10].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 格式化时间
        /// 张雄 create 2011.4.26
        /// </summary>
        /// <returns>格式后的时间</returns>
        private string ForMatDate(string Str)
        {
            string YearStr = String.Empty;

            string MonStr = String.Empty;

            if (!String.IsNullOrEmpty(Str))
            {
                if (Str.Length == 6)
                {
                    //取得年
                    YearStr = Str.Substring(0, 4) + "年";

                    //取得月
                    MonStr = Str.Substring(4, 2);
                    if (MonStr.Substring(0, 1) == "0")
                    {
                        MonStr = MonStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonStr += "月";
                    }
                }
            }
            return Str = YearStr + MonStr;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
        }

        protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.HospitalName = e.CommandArgument.ToString();
                    query.SortSource = Convert.ToInt32(this.droSort.SelectedValue);
                }
                catch { }
                Session[sessionid] = query;
                string url = "HospitalCurrentGoodsList.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

        DataTable dtt = new DataTable();
        /// <summary>
        /// 导出数据
        /// 张雄 create 2011.4.27
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            var model = new HZ.Data.QueryModel();
            model["AreaId"] = sq.AreaID;
            model["HospitalName"] = DAL.DAL_Helper.Replace(txtHospitalname.Text.Trim());
            model["DayTime"] = sq.Mid;

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;

            int t = 0;
            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            System.Data.DataTable dt = iNew.DayTime_SortSorec_HospitalName_GroupBy(model, 1, 0, out t);

            dtt = dt.Copy();
            HZ.Supervise.Web.Function.AddColumn(dtt, "SortSorce");
            HZ.Supervise.Web.Function.ChangeRate(dtt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dtt, "DistributeRate");
            HZ.Excel.ExcelHelper.ExportDataForWeb(dtt, this.gvwSortInfo);
        }
    }
}
